package com.hy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:比较字符串最小字母出现频次
 * author: Mr.Du
 * Date: 2023/6/10
 * Time: 23:49
 */
public class NumSmallerByFrequency {
    
    public static void main(String[] args) {
        String[] queries = {"cbd"};
        String[] words = {"zaaaz"};
        int[] res = numSmallerByFrequency(queries, words);
        System.out.println(Arrays.toString(res));
    }
    
    public static int[] numSmallerByFrequency(String[] queries, String[] words) {
        List<Integer> list = new ArrayList<>();
        int[] nums1 = getNums(queries);
        // Arrays.sort(nums1);
        int[] nums2 = getNums(words);
        Arrays.sort(nums2);
        System.out.println(Arrays.toString(nums2));
        System.out.println(Arrays.toString(nums1));
        for(int i = 0;i < nums1.length;i++){
            int count = 0;
            boolean b = true;
            for(int j = 0;j < nums2.length;j++){
                if(nums1[i] < nums2[j]){
                    count = j;
                    b = false;
                    break;
                }
            }
            if(count == 0 && b){
                list.add(count);
            }else{
                list.add(nums2.length - count);
            }
            
        }
        int[] res = new int[list.size()];
        int index = 0;
        for(int i = 0;i < res.length;i++){
            res[index++] = list.get(i);
        }
        return res;
    }
    
    public static int[] getNums(String[] queries){
        int[] nums = new int[queries.length];
        int index = 0;
        for(String i : queries){
            int[] arr = new int[26];
            for(char c : i.toCharArray()){
                arr[c - 'a']++;
            }
            for(int j = 0;j < 26;j++){
                if(arr[j] != 0){
                    nums[index++] = arr[j];
                    break;
                }
            }
        }
        return nums;
    }
}
